package com.wdg.controller;

import com.wdg.pojo.Reg_user;
import com.wdg.dao.LogDao;
import com.wdg.dao.UserDao;
import com.wdg.dao.impl.LogDaoImpl;
import com.wdg.dao.impl.UserDaoImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

/**
 * @Author : wdg；
 * @Date : 2025/3/30 6:28；
 * @Version : v1.0.0；
 * @Description: 类描述信息；
 **/
@Controller
public class UserController {
    UserDao dao=new UserDaoImpl();
    LogDao ldao=new LogDaoImpl();

    @RequestMapping("lookListJSTL.action")
    public String lookListJSTL(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session=request.getSession();
        Integer grade = (Integer) session.getAttribute("grade");
        String user = (String) session.getAttribute("username");
        Reg_user u = dao.getUser(user);
        ArrayList<Reg_user> list =new ArrayList<>();
        if (grade>=3) {
            list = dao.findList();
        }else {
            list.add(u);
        }
        request.setAttribute("lookListJSTL",list);
        return "lookListJSTL";
    }

    @RequestMapping("delUserById.action")
    public String delUserById(HttpServletRequest request, HttpServletResponse response,Integer id) throws Exception {

        ldao.delLog(id);
        dao.delUserById(id);
        return "redirect:lookListJSTL.action";
    }

    @RequestMapping("findUserById.action")
    public String findUserById(HttpServletRequest request, HttpServletResponse response,Integer id) throws Exception {
        Reg_user u = dao.findUserById(id);
        request.setAttribute("reg_user",u);
        return "modify";
    }

    @RequestMapping("lookList.action")
    public String lookList(HttpServletRequest request, HttpServletResponse response,Integer id) throws Exception {
        ArrayList<Reg_user> list = dao.findList();
        request.setAttribute("lookList",list);
        return "lookList";
    }

    @RequestMapping("modifyUser.action")
    public String modifyUser(HttpServletRequest request, HttpServletResponse response,Reg_user u) throws Exception {
        u.setId(u.getId());
        u.setPw(u.getPw());
        u.setAge(u.getAge());
        dao.modifyUser(u);
        return "redirect:lookListJSTL.action";
    }


    @RequestMapping("reg.action")
    public void reg(HttpServletRequest request, HttpServletResponse response, Reg_user u) throws Exception{
        String user = u.getUser();
        String pw = u.getPw();
        Integer age = u.getAge();
        Integer grade = 1;               // 注册为普通用户
        String s = "";
        if (dao.checkName(user)) {
            s = "你用的'" + user + "'账户名重名了！请换名重新注册！";
        } else {
            u.setUser(user);
            u.setPw(pw);
            u.setAge(age);
            u.setGrade(grade);
            dao.reg_pst_bean(u);
            s = "注册成功！你可以用'" + user + "'账户名进行登录！888";
        }
        request.setAttribute("err", s);
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }
    @RequestMapping("login.action")
    public void login(HttpServletRequest request, HttpServletResponse response,Reg_user u1) throws Exception {
        String user = u1.getUser();
        String pw = u1.getPw();
        HttpSession session=request.getSession();
        Reg_user u = dao.getUser(user);
        if(dao.login(user,pw)){

            // 加入登录日志，写死了

            session.setAttribute("username",user);
            session.setAttribute("grade",u.getGrade());
            response.sendRedirect("welcome.action");
            //    request.getRequestDispatcher("welcome.jsp").forward(request,response);
        }else {
            String s="用户名密码"+pw+"错误，请重新输入123！";
            request.setAttribute("err",s);
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }
    }
    @RequestMapping("logout.action")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session=request.getSession();
        session.invalidate();
        response.sendRedirect("login.jsp");
    }
    @RequestMapping("welcome.action")
    public String welcome(HttpServletRequest request, HttpServletResponse response,Integer id) throws Exception {
        return "welcome";
    }
}
